﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vms.Web.Services.Rrf
{
    public class RtfUtility
    {

        private const string RTF_DOC_FORMAT = "\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033";

        /// <summary>
        /// 1 Red.
        /// 2 Green.
        /// 3 Blue.
        /// 4 Yellow.
        /// 5 White.
        /// </summary>
        private const string COLOR_TBL = "{\\colortbl;\\red255\\green0\\blue0;\\red0\\green255\\blue0;\\red0\\green0\\blue255;\\red255\\green255\\blue0;\\red255\\green255\\blue255;}";
        private const string FONT_TBL = "{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}{\\f1\\fnil\\fcharset0 Times New Roman;}{\\f2\\fnil\\fcharset0 Tahoma;}}";

        /// <summary>
        /// rgb(255, 0, 0) Red.
        /// rgb(0, 128, 0) Green.
        /// rgb(0, 0, 255) Blue.
        /// rgb(255, 255, 0) Yellow.
        /// rgb(255, 255, 255) White. 
        /// </summary>
        private static IDictionary<string, string> DicColor = new Dictionary<string, string>
        {
            {"rgb(255, 0, 0)", "\\cf1"},
            {"rgb(0, 128, 0)", "\\cf2"},
            {"rgb(0, 0, 255)", "\\cf3"},
            {"rgb(255, 255, 0)", "\\cf4"},
            {"rgb(255, 255, 255)", "\\cf5"}
        };

        /// <summary>
        /// arial, time new roman, tahoma.
        /// </summary>
        private static IDictionary<string, string> DicFontFamily = new Dictionary<string, string>
        {
            {"arial", "\\f0"},
            {"times new roman", "\\f1"},
            {"tahoma", "\\f2"},
        };

        /// <summary>
        /// bold, italic
        /// </summary>
        private static IDictionary<string, string> DicCharFormat = new Dictionary<string, string>
        {
            {"bold", "\\b"},
            {"italic", "\\i"}
        };

        private static string toBytesText(string text)
        {
            string result = "";
            int value = 0;
            foreach (char ch in text)
            {
                value = (int)ch;
                result += "\\u" + value + "?";
            }
            return result;
        }

        /// <summary>
        /// Create Rtf document.
        /// </summary>
        /// <param name="fontSize"></param>
        /// <param name="isBold"></param>
        /// <param name="font"></param>
        /// <param name="color"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string createRtfDocument(int fontSize, bool isBold, string font, string color, string text)
        {
            string bold = "";
            if (isBold)
            {
                bold = DicCharFormat["bold"] + " ";
            }
            string result = "{" + RTF_DOC_FORMAT + "\n";
            result += FONT_TBL + "\n";
            result += COLOR_TBL + "\n";
            result += "{\\pard " + "\\fs" + fontSize + " " + bold + DicFontFamily[font.ToLower()] + " " + DicColor[color] + " " + toBytesText(text) + " }\n";
            result += "}";
            return result;
        }

    }
}